package com.cjjd.c.feature.home.chekin.confirm_order;

import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cjjd.c.EventBusKeys;
import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.base.ActionBarActivity;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.bean.OrderBean;
import com.cjjd.c.bean.SelectHomeItemBean;
import com.cjjd.c.feature.home.chekin.pay.PayPledgeActivity;
import com.cjjd.c.http.CallbackImpl;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.LogUtil;
import com.cjjd.c.utils.SharePrefUtil;
import com.cjjd.c.utils.ToastUtil;
import com.cjjd.c.view.NestedExpandaleListView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * ----------------------------------
 * Created by xyd on 2017/12/5.
 * <p/>
 * 描  述：宾客入住单
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class ConfirmOrderActivity extends ActionBarActivity {


    public ArrayList<SelectHomeItemBean> datalist;
    @Bind(R.id.elistview)
    NestedExpandaleListView elistview;
    @Bind(R.id.tv_team_name)
    TextView tvTeamName;
    @Bind(R.id.ll_team_name)
    LinearLayout llTeamName;
    @Bind(R.id.tv_hotel_name)
    TextView tvHotelName;
    @Bind(R.id.cb_ok)
    CheckBox cbOk;
    // 酒店的两个uuid
    private String orderMainUuid;
    private String orderDetailUuid;
    private ConfirmOrderExpandableListAdapter confirmOrderExpandableListAdapter;

    @Override
    public Object getTitleString() {
        return "宾客入住单";
    }

    @Override
    public int getContentView() {
        return R.layout.activity_confirm_order;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        orderMainUuid = getIntent().getStringExtra("mainUuid");
        orderDetailUuid = getIntent().getStringExtra("detailUuid");
        datalist = (ArrayList<SelectHomeItemBean>) getIntent().getSerializableExtra("bean");


        initView();
    }

    private void initView() {

        tvHotelName.setText(getIntent().getStringExtra("name"));


        ArrayList<ArrayList<String>> groupStrings = new ArrayList<>();

        ArrayList<String> groupTittles = new ArrayList<>();


        for (int i = 0; i < datalist.size(); i++) {

            if (datalist.get(i).isChooseRoom.equalsIgnoreCase("1")) {
                groupTittles.add(datalist.get(i).chekInName);
                SelectHomeItemBean bean = datalist.get(i);

                ArrayList<String> itemStrings = new ArrayList<>();
                itemStrings.add(bean.chekInName);
                itemStrings.add(bean.chekInIDCard);
                itemStrings.add(bean.checkInDate);
                itemStrings.add(bean.checkOutDate);
                itemStrings.add(bean.depositMoney + "元");
                itemStrings.add("否");
                itemStrings.add(bean.roomPrice + "元");

                itemStrings.add(bean.roomNo);

                groupStrings.add(itemStrings);

            }
        }


        confirmOrderExpandableListAdapter = new ConfirmOrderExpandableListAdapter(mContext);
        confirmOrderExpandableListAdapter.setGroupData(groupStrings);
        confirmOrderExpandableListAdapter.setGroupTittleData(groupTittles);

        elistview.setAdapter(confirmOrderExpandableListAdapter);

        Display newDisplay = getWindowManager().getDefaultDisplay();
        int width = newDisplay.getWidth();
        elistview.setIndicatorBounds(width - 50, width);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String event) {
        if (EventBusKeys.hadPayCheckIn.equals(event)) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        EventBus.getDefault().unregister(this);
    }

    @OnClick(R.id.btn_confirm_order)
    public void onViewClicked() {
        if(cbOk.isChecked()) {
            toOrderPay();
        }else {
            ToastUtil.showToast("请阅读并酒店预定需知");
        }
    }


    private void toOrderPay() {
        LogUtil.e("xyd", "开始进行预订单支付");
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("orderUuid", orderMainUuid);                 // 预订单主表uuid
        h1.put("orderDetailUuid", orderDetailUuid);         // 预订单明细uuid

        Call<HttpResult<OrderBean>> call = service.toOrderPay(JsonUtils.convertParam("toOrderPay", h1));
        call.enqueue(new CallbackImpl<OrderBean>() {
            @Override
            public void responseSuccess(Call<HttpResult<OrderBean>> call, Response<HttpResult<OrderBean>> response) {

                OrderBean prepayBean = response.body().getData();

                SharePrefUtil.saveString(mContext, ShareKeys.payOrderId, prepayBean.payOrderId);

                Intent intent = new Intent(mContext, PayPledgeActivity.class);
                intent.putExtra("fromPage", PayPledgeActivity.PayCheckIn);
                intent.putExtra("prepaybean", prepayBean);
                mContext.startActivity(intent);
            }
        });
    }
}
